package com.itboye.textileexpres.myset;

import java.util.Locale;

import android.content.Intent;
import android.content.res.Configuration;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.SPUtils;

public class ActivityLanguageChange extends BaseActivity implements
		OnClickListener {
	ImageView back;
	ImageView img_other;
	TextView txt_save;
	RelativeLayout auto, zh, en, jp, tw;
	CheckBox zh_checkbox, en_checkbox, auto_checkbox, tw_checkbox, jp_checkbox;
	String type = "auto";
	TextView title;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_language_change;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		img_other.setVisibility(View.GONE);
		title.setText("语言");
		txt_save.setVisibility(View.VISIBLE);
		type = (String) SPUtils.get(ActivityLanguageChange.this, null,
				SpPublicParams.Language, "auto");
		Log.e("hu...language", type);
		if ((type).equals("auto")) {
			auto_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
		} else if (type.equals("Chinese")) {
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(true);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
		} else if (type.equals("Japan")) {
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(true);
			tw_checkbox.setChecked(false);
		} else if (type.equals("Taiwan")) {
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			tw_checkbox.setChecked(true);
		} else {
			en_checkbox.setChecked(true);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
		}

	}

	private void changeLanguage(String sta) {
		// TODO Auto-generated method stub
		if (sta.equals("Chinese")) {
			Locale.setDefault(Locale.CHINA);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.CHINA;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
		} else if (sta.equals("Taiwan")) {
			Locale.setDefault(Locale.TAIWAN);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.TAIWAN;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());
		} else if (sta.equals("Janpan")) {
			Locale.setDefault(Locale.JAPAN);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.JAPAN;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		} else {
			Locale.setDefault(Locale.ENGLISH);
			Configuration config = getBaseContext().getResources()
					.getConfiguration();
			config.locale = Locale.ENGLISH;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		}

	}

	private void restartActivity() {
		// TODO Auto-generated method stub
		final Intent intent = getPackageManager().getLaunchIntentForPackage(
				getPackageName());
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		// android.os.Process.killProcess(android.os.Process.myPid());
		// System.exit(0);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.txt_save:
			Log.e("hu...", isdefault() + "type," + type);
			// String sta = getResources().getConfiguration().locale
			// .getLanguage();
			// Log.e("hu...", isdefault() + "type," + sta);
			if (isdefault()) {
				changeLanguage(type);
				SPUtils.put(ActivityLanguageChange.this, null,
						SpPublicParams.Language, type);
				restartActivity();
			} else {
				toast("sorry");
				Locale.setDefault(Locale.ENGLISH);
				Configuration config = getBaseContext().getResources()
						.getConfiguration();
				config.locale = Locale.ENGLISH;
				getBaseContext().getResources().updateConfiguration(config,
						getBaseContext().getResources().getDisplayMetrics());
				restartActivity();
			}

			break;
		case R.id.auto:
			type = "auto";
			auto_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.zh:
			type = "Chinese";
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(true);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.en:
			type = "English";
			en_checkbox.setChecked(true);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.tw:
			type = "Taiwan";
			tw_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.jp:
			type = "Japan";
			jp_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			break;

		case R.id.zh_checkbox:
			type = "Chinese";
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(true);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.en_checkbox:
			type = "English";
			en_checkbox.setChecked(true);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.auto_checkbox:
			type = "auto";
			auto_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.tw_checkbox:
			type = "Taiwan";
			tw_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			jp_checkbox.setChecked(false);
			break;
		case R.id.jp_checkbox:
			type = "Japan";
			jp_checkbox.setChecked(true);
			en_checkbox.setChecked(false);
			zh_checkbox.setChecked(false);
			auto_checkbox.setChecked(false);
			tw_checkbox.setChecked(false);
			break;
		default:
			break;
		}
	}

	private boolean isdefault() {
		// Locale locale = getResources().getConfiguration().locale;
		// String language = locale.getLanguage();
		String language = (String) SPUtils.get(ActivityLanguageChange.this,
				null, SpPublicParams.Language, "auto");
		// if (language.endsWith("en") || language.endsWith("cn")
		// || language.endsWith("jp") || language.endsWith("tw")) {
		// return true;
		if (language.equals("English") || language.endsWith("Chinese")
				|| language.endsWith("Japan") || language.endsWith("Taiwan")) {
			return true;
		} else {
			return false;
		}
	}
}
